Package("com.shihua.common.GetValue"); 

com.shihua.common.GetValue={
	init:function(){	
		//com.shihua.common.GetValue.initEvent();	
		com.shihua.common.GetValue.queryData();
		$("#query").bind("click",function(){
			com.shihua.common.GetValue.queryData();
		});
		var listPanel=$("#listPanel");
		var sqlId=listPanel.attr("sqlId");	
		var searchConfig= com.shihua.common.GetValueTableConfig[sqlId].search;
		var searchTd= $("#searchTd");//.empty();
		for (var i = 0; i < searchConfig.length; i++) {
			var type=1==searchConfig[i][2]?"text":"hidden";
			var divObj=$("<div class='item'></div>")
			if(1==searchConfig[i][2]){
				var searchLeble =$("<span>"+searchConfig[i][1]+"</span>");
				divObj.append(searchLeble);				
			}
			var inputObj= $("<input type='"+type+"' id='"+searchConfig[i][0]+"' name='"+searchConfig[i][0]+"' class=\"areaInput\" />  ");
			divObj.append(inputObj);
			searchTd.append(divObj);
			/*if(i!=searchConfig.length-1){
				searchTd.append("<br/>");
			}*/
		}
		$("#cancel").bind("click",function(){
			art.dialog.close();
		});
		$("#enter").bind("click",function(){
			var sqlId=$("#listPanel").attr("sqlId");	
		    var windowType=com.shihua.common.GetValueTableConfig[sqlId].core?com.shihua.common.GetValueTableConfig[sqlId].core.type:"";
			var trObj= $("#listBody").find("tr[default='1']");
			if(trObj&&trObj.size()>0||windowType == "checkbox"){
				var objValue={};
				if (windowType == "checkbox") {
					objValue=new Array();
					trObj.each(function(j,trObj2){
						var trObjValue={};
						$(trObj2).find("td").each(function(i, obj){
							var name = $(obj).attr("name");
							if (name) {
								trObjValue[name] = $(obj).html();
							}
						});
						objValue.push(trObjValue);
					});
				}
				else {
					trObj.find("td").each(function(i, obj){
						var name = $(obj).attr("name");
						if (name) {
							objValue[name] = $(obj).html();
						}
					});
				}
				if(window.dialogArguments){
					window.opener[window.dialogArguments].call(null,objValue);
					window.open('','_self');   
				    window.opener=null;   
				    window.close();   
				}else{
					if (artDialog.open.origin[art.dialog.data("returnFunName")] == undefined) {
						self.parent[art.dialog.data("returnFunName")].call(null, objValue);
					}
					else {
						
						artDialog.open.origin[art.dialog.data("returnFunName")].call(null, objValue);
					}
					art.dialog.close();
				}
			}else{
				art.dialog.tips("请选择表中的数据！");
			}
			
		});
	},initEvent:function(){
		  $("#selectAll").bind("click",function(){    		 
				    		 $("input[name='entityCheckBox']").attr("checked", $("#selectAll").attr("checked")||false);
				    		 var objTrs= $("#listBody").find("tr");
				    		 if(!$("#selectAll").attr("checked")){
				    			objTrs.css("background","#fff");	
				    		 	objTrs.attr("default","0");
							 }else{
								objTrs.css("background","#FFFFCA");	
								objTrs.attr("default","1");
							}
				    	 });	
		//注册TR事件
		$("#listBody").find("tr").hover(
		function(){
			var trObj=$(this);
			if("1"!=trObj.attr("default")){
				trObj.css("background","#e7e7e7");	
			}
		},function(){
			var trObj=$(this);
		
			if("1"!=trObj.attr("default")){
				trObj.css("background","#fff");	
			}
		});
		var sqlId=$("#listPanel").attr("sqlId");	
		var windowType=com.shihua.common.GetValueTableConfig[sqlId].core?com.shihua.common.GetValueTableConfig[sqlId].core.type:"";
		$("#listBody").find("tr").bind("click",function(){
			 if($(this).find("th").size()>0){
			 	return;
			 }			
			 if(windowType=="checkbox"){
			 	 var checkObj= $(this).find("input[type='checkbox']");
							 if($(this).attr("default")==1){
								 checkObj.attr("checked",false);
								 $(this).css("background","#fff");	
								 $(this).attr("default","0");
							 }else{
								 checkObj.attr("checked",true);
								 $(this).css("background","#FFFFCA");	
								 $(this).attr("default","1");
							 }
							
			 }else{
				 $("#listBody").find("tr").each(function(i,obj){
				 	$(obj).attr("default","0");
					 $(this).css("background","#fff");	
				 });
				 $(this).css("background","#FFFFCA");	
				 $(this).attr("default","1");
			 }
		});
		 if (windowType != "checkbox") {
		 	$("#listBody").find("tr").bind("dblclick", function(){
		 	
		 		var objValue = {};
		 		$(this).find("td").each(function(i, obj){
		 			var name = $(obj).attr("name");
		 			if (name) {
		 				objValue[name] = $(obj).html();
		 			}
		 		});
		 		if (window.dialogArguments) {
		 			window.opener[window.dialogArguments].call(null, objValue);
		 			window.close();
		 		}
		 		else {
					if (artDialog.open.origin[art.dialog.data("returnFunName")] == undefined) {
						self.parent[art.dialog.data("returnFunName")].call(null, objValue);
					}
					else {
						artDialog.open.origin[art.dialog.data("returnFunName")].call(null, objValue);
					}
		 			//self.parent[art.dialog.data("returnFunName")].call(null, objValue);
		 			art.dialog.close();
		 		}
		 		
		 	});
		 }
	//查询数据
	},queryData:function(){
		var listPanel=$("#listPanel");
		var sqlId=listPanel.attr("sqlId");	
		var ajax = shihua.common.createAjax();
  		ajax.url = ctx+"/f7/get/"+sqlId;
  		ajax.page=listPanel.find(".pagePanel");
  		ajax.perPage=listPanel.attr("size")||3;
  		ajax.errorDiv=listPanel.find("[key='error']");
  		ajax.waitDiv=listPanel.find("[key='loading']");	
  		listPanel.find("[key='listBody']").empty();
		ajax.beforeSend=function(){
			listPanel.find("[key='listBody']").empty();
			listPanel.find("[key='nodata']").hide();
		}
		ajax.success=function(json){
  			    if (json && json.ret == 0) {
  					if (json.data.length > 0) {	
						var headTr=listPanel.find("[key='listHead']").empty();						
						var tableConfig= com.shihua.common.GetValueTableConfig[sqlId].table;	
						if(!tableConfig){
							art.dialog.alert("系统配置未正确");
							return ;
						}
						var windowType=com.shihua.common.GetValueTableConfig[sqlId].core?com.shihua.common.GetValueTableConfig[sqlId].core.type:"";
						if(windowType=="checkbox"){
							var checkTrObj= $("<th width='30'><input  type='checkbox' id='selectAll'/></td>");
							headTr.append(checkTrObj);
						}
						var headThObj= $("<th>序号</th>");
						headTr.append(headThObj);
						for(var i=0;i<tableConfig.length;i++){
							var isDisplay=0==tableConfig[i][2]?"display:none;":"";
							var headThObj= $("<th style='"+isDisplay+"'>"+tableConfig[i][1]+"</th>");
							headTr.append(headThObj);
						}
						var bodyObj=listPanel.find("[key='listBody']").empty();	
						//初始化列 
						for(var i=0;i<json.data.length;i++){
							var obj=json.data[i];
							var bodyTr=$("<tr></tr>");
							if(windowType=="checkbox"){
								var checkTrObj= $("<td align='center'><input  type='checkbox' name='entityCheckBox'/></td>");
								var dataName= art.dialog.data("dataName");	
								if(null!=dataName&&dataName!=undefined){
								  var trValues= obj[tableConfig[1][0].toUpperCase()];
								  if(dataName.indexOf(trValues)>=0){
									checkTrObj= $("<td align='center'><input  type='checkbox' name='entityCheckBox' checked='checked' /></td>");
									 bodyTr=$("<tr default='1'></tr>");
								    }
								}
								bodyTr.append(checkTrObj);
							}
							var headThObj= $("<td class='getValTd-no'>"+(i+1)+"</td>");
							bodyTr.append(headThObj);
							
							for (var j = 0; j < tableConfig.length; j++) {	
								var trValue= obj[tableConfig[j][0].toUpperCase()];
								 trValue=trValue==0?trValue:(trValue||obj[tableConfig[j][0]]);
								var isDisplay=0==tableConfig[j][2]?"display:none;":"";
								var bodyTd=$("<td name='"+tableConfig[j][0] +"' style='"+isDisplay+"' class='getVal_"+sqlId+"_"+tableConfig[j][0]+"'>"+(trValue==0?trValue:(trValue||""))+"</td>");
								bodyTr.append(bodyTd);
							}
							bodyObj.append(bodyTr);
						}
						
						com.shihua.common.GetValue.initEvent();
  					}else{
  						listPanel.find("[key='nodata']").show();
  					}
  				}
  		}
		var orderBy= com.shihua.common.GetValueTableConfig[sqlId].core.orderBy||"";
		var sqlParams="";		
		 $("#searchTd").find("input").each(function(i,obj){
			if($(obj).val()){
				var op=$(obj).attr("op");
				op=op?op:"~~";
				var _temp = $(obj).attr("name")+op+$(obj).val().trim();
				if( _temp.indexOf("in(")>0 || _temp.indexOf("in (")>0 || sqlParams.indexOf("in(")>0 || sqlParams.indexOf("in (")>0){
					sqlParams+=$(obj).attr("name")+op+$(obj).val().trim()+":";
				}else{
					sqlParams+=$(obj).attr("name")+op+$(obj).val().trim()+",";
				}
				
			}			
		});
		 var data={sqlParams:sqlParams,orderBy:orderBy};
			var sqlIdTemp= $("#sqlId").val()||"";
			if(sqlIdTemp){
				data.sqlId=sqlIdTemp;
			}
  		ajax.send(data);
	}
}
com.shihua.common.GetValue.init();
